package com.wenhai.leetcode.linkedList;

/**
 * https://leetcode-cn.com/problems/reverse-linked-list/
 *
 * @ClassName _206反转链表
 * @Description
 * @Author 谢文海
 * @Date 2021/2/21 23:32
 * @Version 1.0
 **/
public class _206反转链表 {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static ListNode reverseListR(ListNode head) {

        if (head == null || head.next == null) {
            return head;
        }
        ListNode rev = reverseListR(head.next);
        head.next.next = head;
        head.next = null;
        return rev;
    }

    public static ListNode reverseListR2(ListNode head) {
        ListNode newHead = null;
        while (head != null) {
            ListNode last = head.next;
            head.next = newHead;
            newHead = head;
            head = last;
        }
        return newHead;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


}
